package com.lanjie;

import org.springframework.data.relational.core.sql.In;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {

    Integer count = 0;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("有人来了"+(++count));
        //存入到application中
        se.getSession().getServletContext().setAttribute("count",count);

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("有人走了"+(--count));
        //存入到application中
        se.getSession().getServletContext().setAttribute("count",count);
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent se) {
        //登录方法  往session中存入当前用户
            //session.setAttribute("dqyh","张三疯");
        /*if ("dqyh".equals(se.getName())){ //放入的当前用户  说明有人登录了
            System.out.println("有人来了one"+(++count));
            //存入到application中
            se.getSession().getServletContext().setAttribute("count",count);
        }*/
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent se) {
        /*System.out.println("有人走了one"+(--count));
        //存入到application中
        se.getSession().getServletContext().setAttribute("count",count);*/
    }
}
